package cn.bupt.adt.controller.inf;

import cn.bupt.adt.component.ReturnHelper;
import cn.bupt.adt.entity.autogenerate.DoctorAppUser;
import cn.bupt.adt.entity.autogenerate.Order;
import cn.bupt.adt.entity.autogenerate.OrderView;
import cn.bupt.adt.model.OperationContent;
import cn.bupt.adt.service.IAttachedService;
import cn.bupt.adt.service.IDoctorAppUserService;
import cn.bupt.adt.service.IOrderService;
import cn.bupt.adt.util.BeanUtil;
import cn.bupt.adt.util.NetDataAccessUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单状态操作接口
 */
@Controller
@RequestMapping("interface/orderDeal")
public class OrderDoctorControllerInf {

    @Autowired
    IOrderService orderService;

    @Autowired
    IDoctorAppUserService doctorAppUserService;

    @Autowired
    IAttachedService attachedService;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    ReturnHelper returnHelper;

    /**
     * 订单状态更改：2开始手术；3结束手术；4求助手术
     */
    @RequestMapping("/changeOrderStatus")
    @ResponseBody
    public Object changeOrderStatus(HttpServletRequest request, Order order) {
        boolean rst = orderService.updateOrder(order);
        NetDataAccessUtil nau = new NetDataAccessUtil();
        if (rst) {
            nau.setResult(1);
            nau.setResultDesp("修改订单成功");
        } else {
            nau.setResult(0);
            nau.setResultDesp("修改订单失败,请确认是否存在非手术当天开始手术的问题");
        }
        return nau;
    }

    /**
     * 更改订单状态,变5取消订单。0机构取消,1医生取消（订单号、取消方，取消原因）
     */
    @RequestMapping("/cancelOrderByDoc")
    @ResponseBody
    public Object cancelByDoc(HttpServletRequest request, Order order) {
        NetDataAccessUtil nau = new NetDataAccessUtil();
        DoctorAppUser doctorAppUser = doctorAppUserService
                .getDoctorById(orderService.getOrderById(order.getId()).getDoctorId());
        Double attachedMoney = attachedService.getAllAttached().get(0).getDeductionMoney();
        if (orderService.getOrderById(order.getId()).getStatus() == 1
                || (orderService.getOrderById(order.getId()).getStatus() == 5
                && orderService.getOrderById(order.getId()).getCancelBy() == true)) {
            Date nowDate = new Date();// 当前时间\r
            long nowTime = nowDate.getTime();
            long lastTime = (orderService.getOrderById(order.getId()).getStartTime()).getTime();// 以前的时间\r
            long time = nowTime - lastTime;// 时间相减比较。
            Integer id = doctorAppUser.getId();
            if (time < (long) 86400000) {// 1分钟
                double Riskmoney = doctorAppUser.getRiskMargin();
                if (Riskmoney - attachedMoney < 0) {
                    nau.setResult(0);
                    nau.setResultDesp("风险保证金金额不足");
                } else {
                    doctorAppUser.setRiskMargin(Riskmoney - attachedMoney);// .setMoney(Riskmoney-attachedMoney);
                    boolean rst = orderService.cancelOrder(order, id);
                    doctorAppUserService.updateDoctorAppuser(doctorAppUser);
                    if (rst) {
                        nau.setResult(1);
                        nau.setResultDesp("取消订单成功(手术前24小时内取消订单已扣除200元押金)");
                    } else {
                        nau.setResult(0);
                        nau.setResultDesp("取消订单失败");
                    }
                }
            } else {
                boolean rst = orderService.cancelOrder(order, id);
                if (rst) {
                    nau.setResult(1);
                    nau.setResultDesp("取消订单成功");

                } else {
                    nau.setResult(0);
                    nau.setResultDesp("取消订单失败");
                }
            }
        } else {
            nau.setResult(0);
            nau.setResultDesp("已开始手术不能取消订单");
        }
        return nau;
    }

    /**
     * 医生获取订单详情
     *
     * @param order
     * @return
     * @throws IllegalAccessException
     */
    @RequestMapping("/getDocOrderDetail")
    @ResponseBody
    public Object getOrderDetail(OrderView order) throws IllegalAccessException {
        OrderView orderList = orderService.getOrderViewById(order.getId());
        if (orderList == null) {
            return NetDataAccessUtil.error().resultDesp("暂无对应订单");
        }
        Map<String, Object> or = BeanUtil.beanToMap(orderList);
        try {
            or.put("operationContent", objectMapper.readValue(orderList.getOperationContent(), new TypeReference<List<OperationContent>>() {
            }));
        } catch (IOException e) {
            e.printStackTrace();
            return NetDataAccessUtil.error().resultDesp("获取订单详情失败");
        }
        return NetDataAccessUtil.success().resultDesp("获取订单详情成功");
    }
}
